package com.bwie.service.impl;

import cn.hutool.core.util.ReUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.poi.StringUtils;
import com.bwie.pojo.User;
import com.bwie.service.UserService;
import com.bwie.mapper.UserMapper;
import com.bwie.utils.R;
import com.bwie.vo.ForgotVo;
import com.bwie.vo.RegisterVo;
import com.bwie.vo.UserVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
* @author 枉度生
* @description 针对表【t_user】的数据库操作Service实现
* @createDate 2025-08-08 09:51:04
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Autowired
    UserMapper userMapper;


    @Autowired
    StringRedisTemplate stringRedisTemplate;

    private Boolean isMobile(String mobile) {
        String regex = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$";
        return ReUtil.isMatch(regex, mobile);
    }


    private Boolean isEmail(String email) {
        String regex = "^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
        return ReUtil.isMatch(regex, email);
    }

    @Override
    public R UserLogin(UserVo userVo) {
        User userSelectOne=null;
        if (StringUtils.isEmpty(userVo.getUserAccount())){
            return R.ERROR(500,"传来的手机号/邮箱不能为空");
        }
        if (isMobile(userVo.getUserAccount())){
            userSelectOne=userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getPhone,userVo.getUserAccount()));
        } else if (isEmail(userVo.getUserAccount())) {
            userSelectOne=userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getEmail,userVo.getUserAccount()));
        }
        if (userSelectOne==null){
            return R.ERROR(500,"用户不存在");
        }

        String password = userVo.getPassword();
        boolean b = DigestUtil.bcryptCheck(password,userSelectOne.getPassword());
        if (!b){
            return R.ERROR(500,"密码错误");
        }

        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456").claim("userId", userSelectOne.getId()).compact();
        stringRedisTemplate.opsForValue().set("token"+userSelectOne.getId(),token,30, TimeUnit.MINUTES);

        userSelectOne.setToken(token);
        userSelectOne.setPassword(null);

        return R.OK(userSelectOne);
    }

    @Override
    public R register(RegisterVo registerVo) {
        if (StringUtils.isEmpty(registerVo.getMobile())){
            return R.ERROR(500,"手机号不能为空");
        }
        String code = stringRedisTemplate.opsForValue().get("code:" + registerVo.getMobile());
        if (code==null){
            return R.ERROR(500,"验证码已过期");
        }
        if (!code.equals(registerVo.getCode())){
            return R.ERROR(500,"验证码错误");
        }

        User selectOne = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getPhone, registerVo.getMobile()));
        if (selectOne!=null){
            return R.ERROR(500,"手机号已存在");
        }
        String bcrypt = DigestUtil.bcrypt(registerVo.getPassword());
        System.out.println(bcrypt);
        User user = new User();
        user.setPhone(registerVo.getMobile());
        user.setPassword(bcrypt);
        user.setNickName(registerVo.getNickName());
        int insert = userMapper.insert(user);
        if (insert>0){
            return R.OK("添加成功");
        }

        return R.ERROR(500,"添加失败");
    }

    @Override
    public R ForgotPassword(ForgotVo forgotVo) {
        if (StringUtils.isEmpty(forgotVo.getMobile())){
            return R.ERROR(500,"手机号不能为空");
        }
        User userSelect = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getPhone, forgotVo.getMobile()));
        if (userSelect==null){
            return R.ERROR(500,"用户不存在");
        }
        String code = stringRedisTemplate.opsForValue().get("code:" + forgotVo.getMobile());
        if (code==null){
            return R.ERROR(500,"验证码已过期");
        }
        if (!code.equals(forgotVo.getCode())){
            return R.ERROR(500,"验证码错误");
        }
        userSelect.setPassword(DigestUtil.bcrypt(forgotVo.getPassword()));
        int i = userMapper.updateById(userSelect);
        if (i>0){
            return R.OK("修改成功");
        }
        return R.ERROR(500,"修改失败");
    }
}




